<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Admin\Renderable\BannerRender;
use App\Admin\Renderable\IconRender;
use App\Admin\Renderable\SearchRender;
use App\Admin\Renderable\ShopGoodsRender;
use App\Admin\Renderable\TagRender;
use App\Models\Posts\WxTag;
use App\Models\Shop\WxShopClassify;
use App\Models\Shop\WxShopGoods;
use App\Models\WxIcon;
use App\Models\WxSearch;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Models\WxSlideshow;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formMiniTrait{
    public function index_miniprogram(Content $content)
    {
        return $content->title('移动端配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_miniprogram()->render()}</div>
HTML
            );
    }
    protected function form_miniprogram(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        $form->tab('分享', function (Form $form) {
            $form->html(function () {
                return '';
            }, '<b>一、右上角分享</b>');
            $form->text('app_share_right', '默认')->default(Settings::get('app_share_right', '宠也 - 养宠上宠也'));
            $form->text('app_share_circle', '圈子页')->help('其中[circle_name]会被替换为该圈子的名称，[circle_introduce]会被替换为该圈子的简介')->default(Settings::get('app_share_circle', '[circle_name] - 宠也圈子'));
            $form->text('app_share_tag', '话题页')->help('其中[tag_name]会被替换为该话题的名称')->default(Settings::get('app_share_tag', '[tag_name] - 宠也'));
            $form->text('app_share_good', '商品页')->help('其中[good_name]会被替换为该商品的名称')->default(Settings::get('app_share_good', '[good_name] - 宠也商品'));
            $form->text('app_share_user', '我的页面')->help('其中[user_name]会被替换为该用户的昵称，[user_introduce]会被替换为该用户的签名')->default(Settings::get('app_share_user', '[user_name] - 我的主页'));
            $form->text('app_share_author', '他人主页')->help('其中[author_name]会被替换为被分享人的昵称，[author_introduce]会被替换为分享人的简')->default(Settings::get('app_share_author', '[author_name] - 宠也用户'));
            $form->text('app_share_shop', '店铺页')->help('其中[shop_name]会被替换为当前店铺的名字')->default(Settings::get('app_share_shop', '[shop_name] - 宠也店铺'));
            $form->text('app_share_search', '搜索页')->help('其中[search_content]会被替换为搜索内容前20个字符')->default(Settings::get('app_share_search', '[search_content] - 宠也搜索'));
            $form->divider();
            $form->html(function () {
                return '';
            }, '<b>二、按钮分享</b>');
            $form->text('app_share_post', '文章')->help('其中[post_content]会被替换为该文章内容前20字符，[author_name]会被替换为作者的名字')->default(Settings::get('app_share_post', '[post_content] - [author_name] - 宠也'));
        });

        $form->tab('话题', function (Form $form) {
            $form->multipleSelectTable('app_page_hot_tags', '热门话题')
                ->title('话题库')
                ->max(10)
                ->from(TagRender::make())
                ->model(WxTag::class, 'id', 'tags_name', 'selectTable:formMiniTrait:app_page_hot_tags')
                ->help('<a href="https://img.mini.minisns.cn/images/hot_tag_page.jpg" target="_blank">热门话题页面</a>：如果为空，则根据使用次数排序。<a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_page_hot_tags', ''));
                $form->radio('app_topic_list_style', '内容列表样式')->options([0=>'默认',1=>'瀑布流'])->help('话题页内容列表样式')->default(Settings::get('app_topic_list_style', 0));
        });

        $form->tab('图集', function (Form $form) {
            $form->text('app_wallpapers_name', '图集别名')->help('优先显示别名')->default(Settings::get('app_wallpapers_name', ''));
        });
        $form->tab('窗口动画', function (Form $form) {
            $animationtypes = [
                'slide-in-right' => '新窗体从右侧进入',
                'slide-in-left' => '新窗体从左侧进入',
                'slide-in-top' => '新窗体从顶部进入',
                'slide-in-bottom' => '新窗体从底部进入',
                'pop-in' => '新窗体从左侧进入，且老窗体被挤压而出',
                'fade-in' => '新窗体从透明到不透明逐渐显示',
                'zoom-out' => '新窗体从小到大缩放显示',
                'zoom-fade-out' => '新窗体从小到大逐渐放大并且从透明到不透明逐渐显示',
                'none' => '无动画'
            ];
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">Tips: 仅App支持该功能</div>';
            }, '');
            $res19 = __system__paycode__tip__(19);
            Tooltip::make('.mini_global_admin_open_not_support_tip')
                ->bottom()
                ->title($res19['tip']);
            $form->select('app_page_animation_1', '笔记动画')->help($res19['text'])->options($animationtypes)->default(Settings::get('app_page_animation_1', 'slider-in-right'));
            $form->select('app_page_animation_2', '圈子动画')->options($animationtypes)->default(Settings::get('app_page_animation_1', 'slider-in-right'));
            $form->select('app_page_animation_3', '商品动画')->options($animationtypes)->default(Settings::get('app_page_animation_3', 'slider-in-right'));
            $form->select('app_page_animation_4', '用户主页动画')->options($animationtypes)->default(Settings::get('app_page_animation_4', 'slider-in-right'));
            $form->select('app_page_animation_5', '话题动画')->options($animationtypes)->default(Settings::get('app_page_animation_5', 'slider-in-right'));
            $form->select('app_page_animation_7', '商品分类动画')->options($animationtypes)->default(Settings::get('app_page_animation_7', 'slider-in-right'));
            $form->select('app_page_animation_8', '帮助问答动画')->options($animationtypes)->default(Settings::get('app_page_animation_8', 'slider-in-right'));
            $form->select('app_page_animation_10', 'webview动画')->options($animationtypes)->default(Settings::get('app_page_animation_10', 'slider-in-right'));
            $form->select('app_page_animation_12', '用户名片动画')->options($animationtypes)->default(Settings::get('app_page_animation_12', 'slider-in-right'));
            $form->select('app_page_animation_18', '聊天界面动画')->options($animationtypes)->default(Settings::get('app_page_animation_18', 'slider-in-right'));
            $form->select('app_page_animation_20', '图集界面动画')->options($animationtypes)->default(Settings::get('app_page_animation_20', 'slider-in-right'));
            $form->select('app_page_animation_21', '图集主题界面动画')->options($animationtypes)->default(Settings::get('app_page_animation_21', 'slider-in-right'));

            $form->select('app_page_animation_6', '其他页面动画')->options($animationtypes)->default(Settings::get('app_page_animation_6', 'slider-in-right'));

        });

        $form->tab('ip归属地', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">Tips: 需要先配置API，点击<a href="'.admin_url('settings/api').'" target="_blank">API配置</a></div>';
            }, '');
            $form->radio('app_ip_comment_show', '评论归属地')->options([0=>'不显示', 1=>'国家', 2=>'省份', 3=>'城市', 4=>'县城'])->default(Settings::get('app_ip_comment_show', 3));
            $form->radio('app_ip_post_show', '笔记归属地')->options([0=>'不显示', 1=>'国家', 2=>'省份', 3=>'城市', 4=>'县城'])->default(Settings::get('app_ip_post_show', 3));
            $form->radio('app_ip_user_show', '用户归属地')->options([0=>'不显示', 1=>'国家', 2=>'省份', 3=>'城市', 4=>'县城'])->default(Settings::get('app_ip_user_show', 3));
        });


        $form->action('settings/action/miniprogram');
        return $form;
    }

    public function miniprogram(Request $request){
        SiteUtils::forget_config_data();

        // 4
        Settings::set('app_share_right', $request->input('app_share_right'));
        Settings::set('app_share_user', $request->input('app_share_user'));
        Settings::set('app_share_author', $request->input('app_share_author'));
        Settings::set('app_share_shop', $request->input('app_share_shop'));
        Settings::set('app_share_circle', $request->input('app_share_circle'));
        Settings::set('app_share_tag', $request->input('app_share_tag'));
        Settings::set('app_share_good', $request->input('app_share_good'));
        Settings::set('app_share_search', $request->input('app_share_search'));
        Settings::set('app_share_post', $request->input('app_share_post'));





        Settings::set('app_page_hot_tags', $request->input('app_page_hot_tags'), true);
        Settings::set('app_topic_list_style', $request->input('app_topic_list_style'), true);


        Settings::set('app_wallpapers_name', $request->input('app_wallpapers_name'), true);

        Cache::forget('app_share_format');
        $app_share_format = [
            'app_share_right' => $request->input('app_share_right'),
            'app_share_user' => $request->input('app_share_user'),
            'app_share_author' => $request->input('app_share_author'),
            'app_share_shop' => $request->input('app_share_shop'),
            'app_share_circle' => $request->input('app_share_circle'),
            'app_share_tag' => $request->input('app_share_tag'),
            'app_share_good' => $request->input('app_share_good'),
            'app_share_search' => $request->input('app_share_search'),
            'app_share_post' => $request->input('app_share_post'),
        ];
        Cache::put('app_share_format', json_encode($app_share_format));


        $animation = [
            "animation_1" => $request->input('app_page_animation_1'),
            "animation_2" => $request->input('app_page_animation_2'),
            "animation_3" => $request->input('app_page_animation_3'),
            "animation_4" => $request->input('app_page_animation_4'),
            "animation_5" => $request->input('app_page_animation_5'),
            "animation_6" => $request->input('app_page_animation_6'),
            "animation_7" => $request->input('app_page_animation_7'),
            "animation_8" => $request->input('app_page_animation_8'),
            "animation_10" => $request->input('app_page_animation_10'),
            "animation_12" => $request->input('app_page_animation_12'),
            "animation_18" => $request->input('app_page_animation_18'),
            "animation_20" => $request->input('app_page_animation_20'),
            "animation_21" => $request->input('app_page_animation_21'),
        ];
        Cache::forget('app_page_animation');
        Cache::put('app_page_animation', json_encode($animation));


        Settings::set('app_page_animation_1', $request->input('app_page_animation_1'), true);
        Settings::set('app_page_animation_2', $request->input('app_page_animation_2'), true);
        Settings::set('app_page_animation_3', $request->input('app_page_animation_3'), true);
        Settings::set('app_page_animation_4', $request->input('app_page_animation_4'), true);
        Settings::set('app_page_animation_5', $request->input('app_page_animation_5'), true);
        Settings::set('app_page_animation_6', $request->input('app_page_animation_6'), true);
        Settings::set('app_page_animation_7', $request->input('app_page_animation_7'), true);
        Settings::set('app_page_animation_8', $request->input('app_page_animation_8'), true);
        Settings::set('app_page_animation_10', $request->input('app_page_animation_10'), true);
        Settings::set('app_page_animation_12', $request->input('app_page_animation_12'), true);
        Settings::set('app_page_animation_18', $request->input('app_page_animation_18'), true);
        Settings::set('app_page_animation_20', $request->input('app_page_animation_20'), true);
        Settings::set('app_page_animation_21', $request->input('app_page_animation_21'), true);

        Settings::set('app_ip_comment_show', $request->input('app_ip_comment_show'), true);
        Settings::set('app_ip_post_show', $request->input('app_ip_post_show'), true);
        Settings::set('app_ip_user_show', $request->input('app_ip_user_show'), true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
